<template>
  <t-dialog
    :header="header"
    :visible.sync="visible"
    :onClose="closeDialog"
    :closeOnOverlayClick="false"
    @confirm="onSubmit"
    :confirmBtn="{
        content: loading?'提交中...':'确认',
        theme: 'primary',
        loading: loading,
      }"
    width="60%"
    top="5%"
  >
    <div style="height: 65vh">
      <t-form ref="form" :colon="true" :data="formData" labelWidth="100px" :rules="rules">
        <t-form-item label="子系统" name="type">
          <t-select clearable v-model="formData.serviceId" :keys="{label:'serviceName',value:'id'}"
                    style="width: 390px" :options="serviceList"></t-select>
        </t-form-item>
        <t-form-item label="模版类型" name="type">
          <t-select style="width: 390px" placeholder="模版类型" :options="dict.type.msg_template_type"
                    v-model="formData.type"></t-select>
        </t-form-item>
        <t-form-item label="模版内容" name="content">
          <t-textarea v-model="formData.content" :autosize="{minRows: 5}"/>
        </t-form-item>
<!--        <t-form-item label="备注" name="note">-->
<!--          <t-textarea v-model="formData.note" :autosize="{minRows: 3}"/>-->
<!--        </t-form-item>-->
        <t-form-item label="是否启用" name="status">
          <t-radio-group :options="[{label:'启用',value:'1'},{label:'关闭',value:'0'}]" v-model="formData.status"></t-radio-group>
        </t-form-item>
      </t-form>
    </div>
  </t-dialog>
</template>

<script>
import { sysServiceBusinessList } from "@/api/system/role";

export default {
  name: "add-edit-template",
  dicts: ['msg_template_type'],
  props: ['header', 'objId'],
  data(){
    return {
      visible: true,
      loading: false,
      formData:{
        type: '',
        status: '1',
        content: '',
        note: '',
        serviceId: null
      },
      rules:{
        type: [
          { required: true, message: "模版类型不能为空" }
        ],
        content: [
          { required: true, message: "模版内容不能为空" }
        ],
        status: [
          { required: true, message: "是否启用不能为空" }
        ],
      },
      serviceList: []
    }
  },
  watch: {
    objId: {
      immediate: true,
      handler(val) {
        if (val) {
          this.getObj();
        }
      }
    },
  },
  created() {
    this.getServiceList();
  },
  methods:{
    getServiceList() {
      sysServiceBusinessList().then((res) => {
        this.serviceList = res.data;
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    getObj() {
      this.$api.system.sms.templateGet(this.objId).then(response => {
        Object.assign(this.formData, response.data)
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    closeDialog() {
      this.$emit("update:visible", false);
    },
    onSubmit() {
      this.$refs.form.validate().then((validateResult) => {
        if (validateResult === true) {
          this.loading = true;
          if (this.objId) {
            this.$api.system.sms.templateUpdate(this.formData).then(() => {
              this.$message.success("修改成功");
              this.$emit("reload");
              this.$emit("update:visible", false);
              this.loading = false;
            }).catch((e) => {
              this.loading = false;
              this.$message.error(e.toString());
            });
          } else {
            this.$api.system.sms.templateAdd(this.formData).then((response) => {
              this.$message.success("新增成功");
              this.$emit("reload");
              this.$emit("update:visible", false);
              this.loading = false;
            }).catch((e) => {
              this.loading = false;
              this.$message.error(e.toString());
            });
          }
        }else {
          const firstError = Object.values(validateResult)[0]?.[0]?.message;
          this.$message.error(firstError);
        }
      });
    },
  }
}
</script>

<style scoped>

</style>
